<?php
class member extends MY_Controller{
    function member(){
        parent::MY_Controller();
        $this->data['title'] = "Master Data Member";
        $this->load->model('m_member');
        $this->load->model('m_jenis_member');
        $this->load->model('m_pekerjaan');
        $this->load->model('m_relasi_kakak_pembina');
        $this->load->model('m_relasi_donatur_tunggal');
    }
    
    var $data;
    var $limit = 10;
    
    function index(){
        $this->get_member('all','nama','asc',0);
    }
    function search(){
        $params = "nama_".$this->input->post('nama');
        $url = $this->input->post('url');
        redirect("$url/$params/0/");
    }
    function get_member($params = "all",$sortBy,$type = 'asc',$offset = 0){
        $option = parsing_param1($params);
        $this->data['searchLink'] = "member/get_member";
        $this->data['content'] = "member/v_member";
        
        $uri_segment = 6;
        if($offset != 0){
            $offset = $this->uri->segment($uri_segment);
        }
        $num_rows = $this->m_member->getCountRows($option);
        $this->data['listMember'] = $this->m_member->getListMember($offset,$this->limit,$option,$sortBy,$type);
        $config['base_url'] = site_url("member/get_member")."/".$params."/$sortBy/".$type;
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        if($type == 'asc'){
            $type = 'desc';
        }else $type = 'asc';
        $this->data['sortNama'] = site_url("member/get_member/".$id)."/".$params."/nama/$type/$offset";
        
        $this->render($this->data);
    }
    function add_member_form($params = "all",$sortBy,$type = 'asc',$offset = 0){
        $option = parsing_param1($params);
        $this->data['searchLink'] = "member/add_member_form";
        
        $this->data['content'] = "member/v_member";
        $this->data['subLink'] = "member/add_member_proses";
        $this->data['formTitle'] = "Form Tambah Data Member";
        $this->data['formView'] = "member/addMemberForm";
        
        $uri_segment = 6;
        if($offset != 0){
            $offset = $this->uri->segment($uri_segment);
        }
        $num_rows = $this->m_member->getCountRows($option);
        $this->data['listMember'] = $this->m_member->getListMember($offset,$this->limit,$option,$sortBy,$type);
        $this->data['listJenisMember'] = $this->m_jenis_member->getListJenisMember();
        $this->data['listPekerjaan'] = $this->m_pekerjaan->getListPekerjaan();
        $this->data['countJenisMember'] = $this->m_jenis_member->getCountRows();
        $config['base_url'] = site_url("member/add_member_form")."/".$params."/$sortBy/".$type;
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        if($type == 'asc'){
            $type = 'desc';
        }else $type = 'asc';
        $this->data['sortNama'] = site_url("member/add_member_form/".$id)."/".$params."/nama/$type/$offset";
        
        $this->render($this->data);
    }
    function edit_member_form($id,$params="all",$sortBy,$type = 'asc',$offset = 0){
        $option = parsing_param1($params);
        $this->data['searchLink'] = "member/edit_member_form/$id";
        
        $this->data['content'] = "member/v_member";
        $this->data['subLink'] = "member/edit_member_proses";
        $this->data['formTitle'] = "Form Edit Data Member";
        $this->data['formView'] = "member/editMemberForm";
        $this->data['dataInput'] = $this->m_member->getMemberById($id);
        
        $uri_segment = 7;
        if($offset != 0){
            $offset = $this->uri->segment($uri_segment);
        }
        $num_rows = $this->m_member->getCountRows($option);
        $this->data['listMember'] = $this->m_member->getListMember($offset,$this->limit,$option,$sortBy,$type);
        $this->data['listJenisMember'] = $this->m_jenis_member->getListJenisMember();
        $this->data['listPekerjaan'] = $this->m_pekerjaan->getListPekerjaan();
        $this->data['countJenisMember'] = $this->m_jenis_member->getCountRows();
        $config['base_url'] = site_url("member/edit_member_form/".$id)."/".$params."/$sortBy/".$type;
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        if($type == 'asc'){
            $type = 'desc';
        }else $type = 'asc';
        $this->data['sortNama'] = site_url("member/edit_member_form/".$id)."/".$params."/nama/$type/$offset";
        
        $this->render($this->data);
    }
    function add_member_proses(){
        $insert = $this->m_member->addMember($_POST);
        if($insert == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("member");
    }
    function edit_member_proses(){
        $update = $this->m_member->editMember($_POST);
        if($update == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("member");
    }
    function add_dinamis_member_proses(){
        $data = array('tanggal' => date('Y-m-d'),
            'id_member' => $this->input->post('idMember'),
            'alamat' => $this->input->post('alamat'),
            'no_telp' => $this->input->post('noTelp'),
            'id_pekerjaan' => $this->input->post('pekerjaan'),
            'status' => '1');
        
        $insert = $this->m_member->addDinamisMember($data);
        if($insert == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("member/detail_member/".$this->input->post('idMember'));
    }
    function detail_member($id){
        $this->data['content'] = "member/detailMember";
        $this->data['listMember'] = $this->m_member->getMemberById($id);
        $this->data['cekDonaturTunggal'] = $this->m_member->cekDonaturTunggal($id);
        $this->data['cekKakakPembina'] = $this->m_member->cekKakakPembina($id);
        $this->data['listPekerjaan'] = $this->m_pekerjaan->getListPekerjaan();
        $this->data['linkBack'] = site_url('member/get_member');
        $this->data['titlePage'] = "Detail Member";
        $this->data['subLink'] = "member/add_dinamis_member_proses";
        $this->data['dinamisMember'] = $this->m_member->getDinamisMember($id);
        
        $this->render($this->data);
    }
    function delete_member($id){
        $delete = $this->m_member->deleteMember($id);
        if($delete == TRUE){
            $this->session->set_flashdata("message",3);
        }else{
            $this->session->set_flashdata("message",4);
        }
        
        redirect("member");
    }
    function relasi_donatur_tunggal($id){
        $this->data['content'] = "member/relasiDonaturTunggal";
        $this->data['linkBack'] = site_url('member/get_member');
        $this->data['linkBack2'] = site_url('member/detail_member/'.$id);
        $this->data['titlePage'] = "Relasi Donatur Tunggal";
        $this->data['formTitle'] = "Form Relasi Donatur Tunggal";
        $this->data['listMember'] = $this->m_member->getMemberById($id);
        $this->data['subLink'] = "member/relasi_donatur_tunggal_proses";
        $this->data['listAdikDonasi'] = $this->m_relasi_donatur_tunggal->getRelasiDonaturTunggal($id);
        
        $this->render($this->data);
    }
    function relasi_kakak_pembina($id){
        $this->data['content'] = "member/relasiKakakPembina";
        $this->data['linkBack'] = site_url('member/get_member');
        $this->data['linkBack2'] = site_url('member/detail_member/'.$id);
        $this->data['titlePage'] = "Relasi Kakak Pembina";
        $this->data['formTitle'] = "Form Relasi Kakak Pembina";
        $this->data['listMember'] = $this->m_member->getMemberById($id);
        $this->data['subLink'] = "member/relasi_kakak_pembina_proses";
        $this->data['listAdikAsuh'] = $this->m_relasi_kakak_pembina->getRelasiKakakPembina($id);
        
        $this->render($this->data);
    }
    function relasi_donatur_tunggal_proses(){
        $id_adik_asuh = $this->input->post('id_adik_asuh');
        $id_member = $this->input->post('id_member');
        $data = array('id_adik_asuh' => $id_adik_asuh,
            'id_member' => $id_member);
        $insert = $this->m_relasi_donatur_tunggal->addRelasiDonaturTunggal($data);
        if($insert == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("member/relasi_donatur_tunggal/$id_member");
    }
    function relasi_kakak_pembina_proses(){
        $id_adik_asuh = $this->input->post('id_adik_asuh');
        $id_member = $this->input->post('id_member');
        $data = array('id_adik_asuh' => $id_adik_asuh,
            'id_member' => $id_member);
        $insert = $this->m_relasi_kakak_pembina->addRelasiKakakPembina($data);
        if($insert == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("member/relasi_kakak_pembina/$id_member");
    }
    function delete_relasi_kakak_pembina($id,$idMember){
        $delete = $this->m_relasi_kakak_pembina->deleteRelasiKakakPembina($id);
        if($delete == TRUE){
            $this->session->set_flashdata("message",3);
        }else{
            $this->session->set_flashdata("message",4);
        }
        
        redirect("member/relasi_kakak_pembina/$idMember");
    }
    function delete_relasi_donatur_tunggal($id,$idMember){
        $delete = $this->m_relasi_donatur_tunggal->deleteRelasiDonaturTunggal($id);
        if($delete == TRUE){
            $this->session->set_flashdata("message",3);
        }else{
            $this->session->set_flashdata("message",4);
        }
        
        redirect("member/relasi_donatur_tunggal/$idMember");
    }
}
?>
